package com.zilue.payment.gateway.manage;

import com.zilue.payment.gateway.domain.CorpDO;
import com.zilue.payment.gateway.domain.PayOrderDO;
import com.zilue.payment.gateway.paramter.order.QueryParameter;
import com.zilue.payment.gateway.reurndata.PublicReturn;
import com.zilue.payment.gateway.reurndata.enums.BusinessStatusEnum;
import com.zilue.payment.gateway.reurndata.enums.RequestStatusEnum;
import com.zilue.payment.gateway.reurndata.order.QueryReturn;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zilue on 2017/3/9.
 */
@Service
public class OrderQueryManager extends BaseManager {

    public PublicReturn query(CorpDO corpDO,QueryParameter queryParameter) {
        QueryReturn queryReturn = new QueryReturn(RequestStatusEnum.RequestSuccess);
        PayOrderDO payOrder = queryByOrderNoOutTradeNo(queryParameter.getOrder_no(), queryParameter.getOut_trade_no(), queryParameter.getOut_pay_no(), corpDO.getParentCorpId(), corpDO.getCorpId());
        if (payOrder == null) {
            queryReturn.setBusinessStatus(BusinessStatusEnum.OrderNotExist);
            return queryReturn;
        }
        queryReturn.setBusinessStatus(BusinessStatusEnum.OrderQuerySuccess);
        queryReturn.setOut_trade_no(payOrder.getOutTradeNo());
        queryReturn.setOut_pay_no(payOrder.getOutPayNo());
        queryReturn.setOrder_no(payOrder.getOrderNo());
        queryReturn.setTotal_amount(payOrder.getTotalAmount());
        queryReturn.setSubject(payOrder.getSubject());
        queryReturn.setTrade_status(PayOrderDO.STATUS_MAP.get(payOrder.getStatus()));
        Date payDate = payOrder.getPayDate();
        if (payDate != null) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            queryReturn.setPayment_time(format.format(payDate));
        }
        return queryReturn;
    }
}
